package com.ruoyi.web.controller.member.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.VO.member.MemberExportVO;
import com.ruoyi.common.entity.VO.member.MemberSearchVO;
import com.ruoyi.common.entity.VO.member.MemberVO;
import com.ruoyi.common.entity.VO.shop.ShopSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.member.service.IBackMemberService;
import com.ruoyi.web.controller.shop.service.IBackShopService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员管理
 */
@Controller
@RequestMapping("/back/member")
public class BackMemberController extends BaseController {
    private String prefix = "member";

    @Resource
    private IBackMemberService memberService;

    @Resource
    private IBackShopService shopService;

    /**
     * 会员列表-页面跳转
     *
     * @return
     */
    @RequiresPermissions("back:member:list")
    @GetMapping()
    public String list(ModelMap mmap) {
        ShopSearchVO shopSearchVO = new ShopSearchVO();
        shopSearchVO.setIsDelete(0);
        shopSearchVO.setReviewStatus(1);
        List<ShopVO> shopList = shopService.searchList(shopSearchVO);

        mmap.put("shopList",shopList);
        return prefix + "/list";
    }

    /**
     * 查询会员列表-数据
     */
    @RequiresPermissions("back:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MemberSearchVO memberSearchVO) {
        startPage();
        if(ShiroUtils.getShopId()!=0){
            memberSearchVO.setShopId(ShiroUtils.getShopId());
        }
        List<MemberVO> list = memberService.searchList(memberSearchVO);
        return getDataTable(list);
    }

    /**
     * 查看会员信息-页面跳转
     */
    @RequiresPermissions("back:member:view")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MemberDO member = memberService.selectById(id);
        mmap.put("member", member);
        return prefix + "/edit";
    }

    /**
     * 编辑会员-数据
     */
    @Log(title = "总后台-会员管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:member:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(MemberVO memberVO) {
        try {
            memberVO.setUpdateBy(ShiroUtils.getSysUser().getUserName());
            memberVO.setUpdateTime(new Date());
            memberService.edit(memberVO);
            return success();
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 数据处理-会员导出
     *
     * @return
     */
    @PostMapping("/export")
    @Log(title = "总后台-会员管理", businessType = BusinessType.EXPORT)
    @ResponseBody
    public AjaxResult export(MemberSearchVO memberSearchVO) {
        if (StringUtils.isNotEmpty(memberSearchVO.getMemberIdStr())) {
            if (memberSearchVO.getMemberIdStr().contains(",")) {
                Long[] memberIdArr = Convert.toLongArray(memberSearchVO.getMemberIdStr());
                if (memberIdArr.length > 100) {
                    error("导出数据不能超过100条！");
                }
                memberSearchVO.setMemberIdArr(memberIdArr);
            }
        }
        String sheetName = "";
        Map<String, Object> map = memberSearchVO.getParams();
        if (map.containsKey("beginTime") && map.containsKey("endTime")) {
            sheetName += map.get("beginTime") + "到" + map.get("endTime") + "注册的会员";
        } else if (map.containsKey("month")) {
            sheetName += map.containsKey("month") + "月份注册的会员";
        } else {
            sheetName += "自定义导出会员";
        }
        List<MemberExportVO> orderList = memberService.searchMemberExportList(memberSearchVO);
        ExcelUtil<MemberExportVO> util = new ExcelUtil<>(MemberExportVO.class);
        return util.exportExcel(orderList, sheetName);
    }

    /**
     * 删除会员
     */
    @Log(title = "总后台-会员管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("back:member:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(memberService.removeMemberByIds(ids));
    }

}
